/*
 * BanBu_PersonCardController
 * @description 本文件提供邀请微博好友分享到平台的个人名片的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		***
 * @description 本文件提供邀请微博好友分享到平台的个人名片的ViewController
 */

#import "BanBu_PersonCardController.h"
#import "UMSocial.h"
@interface BanBu_PersonCardController ()
{
    UIImageView * _bigShareImgV;
}
@end

@implementation BanBu_PersonCardController
@synthesize shareType;

- (void)dealloc
{
    [self.shareType release];
    self.shareType = nil;
    [_bigShareImgV release];
    _bigShareImgV = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIImageView * backImg = [[UIImageView alloc]initWithFrame:self.view.frame];
    NSString * backPath = [[NSBundle mainBundle]pathForResource:@"basemap_1" ofType:@"png"];
    backImg.image = [UIImage imageWithContentsOfFile:backPath];
    [self.view addSubview:backImg];
    [backImg release];
    UIImageView * backimg2 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 104, 280, 131)];
    NSString * backPath2 = [[NSBundle mainBundle]pathForResource:@"basemap_2" ofType:@"png"];
    backimg2.image = [UIImage imageWithContentsOfFile:backPath2];
    [self.view addSubview:backimg2];
    [backimg2 release];
    UIButton * closeBu = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBu setFrame:CGRectMake(9, 26, 60, 60)];
    [closeBu setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 30, 30)];
    NSString * closePath = [[NSBundle mainBundle] pathForResource:@"share_close" ofType:@"png"];
    [closeBu setImage:[UIImage imageWithContentsOfFile:closePath] forState:UIControlStateNormal];
    [self.view addSubview:closeBu];
    [closeBu addTarget:self action:@selector(dissTolastView) forControlEvents:UIControlEventTouchUpInside];
    
    //头像
    UIImageView * iconImg = [[UIImageView alloc]initWithFrame:CGRectMake(3, 63, 53, 53)];
    iconImg.layer.cornerRadius = 26.5;
    iconImg.layer.masksToBounds = YES;
    iconImg.layer.borderColor = [[UIColor whiteColor]CGColor];
    iconImg.layer.borderWidth = 1.0;
    [backimg2 addSubview:iconImg];
    [iconImg release];
    NSDictionary *profile = [NSDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
    [iconImg setImageWithURL:[NSURL URLWithString:[profile valueForKey:@"uface"]] placeholderImage:[UIImage imageNamed:@"default.png"] andType:0];
    
    //名字
    UILabel * nameLa = [[UILabel alloc]initWithFrame:CGRectMake(63, 98, 100, 13)];
    nameLa.backgroundColor = [UIColor clearColor];
    nameLa.textColor = [UIColor whiteColor];
    [backimg2 addSubview:nameLa];
    [nameLa release];
    nameLa.text = [profile valueForKey:@"pname"];
    nameLa.font = [UIFont boldSystemFontOfSize:10.0];
    [nameLa sizeToFit];
    
    //性别
    UIImageView * sexImg = [[UIImageView alloc]initWithFrame:CGRectMake(nameLa.frame.origin.x + nameLa.frame.size.width + 10, 100, 9, 9)];
    NSString * sexPath = [[NSBundle mainBundle] pathForResource:[profile valueForKey:@"gender"] ofType:@"png"];
    sexImg.image = [UIImage imageWithContentsOfFile:sexPath];
    [backimg2 addSubview:sexImg];
    [sexImg release];
    
    //年龄
    UILabel * ageLa = [[UILabel alloc]initWithFrame:CGRectMake(nameLa.frame.origin.x + nameLa.frame.size.width + 20, 98, 15, 13)];
    ageLa.backgroundColor = [UIColor clearColor];
    ageLa.font = [UIFont boldSystemFontOfSize:10.0];
    ageLa.textColor = [UIColor whiteColor];
    ageLa.text = [profile valueForKey:@"oldyears"];
    [backimg2 addSubview:ageLa];
    [ageLa release];
    
    //所在地
    UILabel * addressLa = [[UILabel alloc]initWithFrame:CGRectMake(nameLa.frame.origin.x + nameLa.frame.size.width + 42, 98, 140, 13)];
    addressLa.backgroundColor = [UIColor clearColor];
    addressLa.font = [UIFont boldSystemFontOfSize:10.0];
    addressLa.textColor = [UIColor whiteColor];
    addressLa.text = [profile valueForKey:@"incityofname"];
    [backimg2 addSubview:addressLa];
    [addressLa release];
    
    //半圆号
    UILabel * idLa = [[UILabel alloc]initWithFrame:CGRectMake(63, 115, 150, 13)];
    idLa.backgroundColor = [UIColor clearColor];
    idLa.font = [UIFont boldSystemFontOfSize:10.0];
    idLa.textColor = [UIColor whiteColor];
    idLa.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"s0c0", nil),MyAppDataManager.useruid];
    [backimg2 addSubview:idLa];
    [idLa release];
    
    //分享按钮
    UIButton * shareBu = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBu setFrame:CGRectMake(59, __MainScreen_Height - 69, 200, 35)];
    [shareBu setBackgroundImage:[UIImage imageNamed:@"shareToWB.png"] forState:UIControlStateNormal];
    [shareBu setTitle:NSLocalizedString(@"shareButton", nil) forState:UIControlStateNormal];
    [self.view addSubview:shareBu];
    [shareBu addTarget:self action:@selector(shareBuPressed) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 分享按钮触发方法
/**
 * @函数名称：shareBuPressed
 * @函数描述：
 * @输入参数：
 * @输出参数：
 * @返回值：
 * @author：yinlinlin
 */
- (void)shareBuPressed
{
    if (!_bigShareImgV)
    {
        [self getNewBigShareImg];
    }
    NSString * shareUrl;
    if ([UserDefaults valueForKey:@"user_shortUrl"])
    {
        shareUrl = (NSString *)[UserDefaults valueForKey:@"user_shortUrl"];
    }
    else
    {
        shareUrl = [NSString stringWithFormat:@"http://www.halfarea.com/shareto/userview.html?userid=%@",MyAppDataManager.useruid];
    }
    
    NSString * shareText = [NSString stringWithFormat:@"%@ %@。%@ %@",NSLocalizedString(@"invite_pre", nil),MyAppDataManager.useruid,NSLocalizedString(@"koochatComment", nil),shareUrl];
    
    UIImage * shareImg = [self getImageFromScreenWithFrame:_bigShareImgV.bounds andView:_bigShareImgV];
    __block BanBu_PersonCardController * bself = self;
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[self.shareType] content:shareText image:shareImg location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            NSLog(@"分享成功！");
            [TKLoadingView showTkloadingAddedTo:bself.view title:NSLocalizedString(@"shareSuccess", nil) activityAnimated:YES duration:1.0];
            [bself performSelector:@selector(dissTolastView) withObject:nil afterDelay:1.0];
            
        }
    }];

}

- (void)getNewBigShareImg
{
    _bigShareImgV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 104, 620, 131 * 2)];
    NSString * backPath2 = [[NSBundle mainBundle]pathForResource:@"big_base_map" ofType:@"png"];
    _bigShareImgV.image = [UIImage imageWithContentsOfFile:backPath2];
    
    //头像
    UIImageView * iconImg = [[UIImageView alloc]initWithFrame:CGRectMake(8, 63 * 2, 53 * 2, 53 * 2)];
    iconImg.layer.cornerRadius = 26.5 * 2;
    iconImg.layer.masksToBounds = YES;
    iconImg.layer.borderColor = [[UIColor whiteColor]CGColor];
    iconImg.layer.borderWidth = 3.0;
    [_bigShareImgV addSubview:iconImg];
    [iconImg release];
    NSDictionary *profile = [NSDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
    [iconImg setImageWithURL:[NSURL URLWithString:[profile valueForKey:@"uface"]] placeholderImage:[UIImage imageNamed:@"default.png"] andType:0];
    
    //名字
    UILabel * nameLa = [[UILabel alloc]initWithFrame:CGRectMake(63 * 2, 98 * 2, 100 * 2, 13 * 2)];
    nameLa.backgroundColor = [UIColor clearColor];
    nameLa.textColor = [UIColor whiteColor];
    [_bigShareImgV addSubview:nameLa];
    [nameLa release];
    nameLa.text = [profile valueForKey:@"pname"];
    nameLa.font = [UIFont boldSystemFontOfSize:20.0];
    [nameLa sizeToFit];
    
    //性别
    UIImageView * sexImg = [[UIImageView alloc]initWithFrame:CGRectMake(nameLa.frame.origin.x + nameLa.frame.size.width + 20, 200, 18, 18)];
    NSString * sexPath = [[NSBundle mainBundle] pathForResource:[profile valueForKey:@"gender"] ofType:@"png"];
    sexImg.image = [UIImage imageWithContentsOfFile:sexPath];
    [_bigShareImgV addSubview:sexImg];
    [sexImg release];
    
    //年龄
    UILabel * ageLa = [[UILabel alloc]initWithFrame:CGRectMake(nameLa.frame.origin.x + nameLa.frame.size.width + 40, 98 * 2, 30, 26)];
    ageLa.backgroundColor = [UIColor clearColor];
    ageLa.font = [UIFont boldSystemFontOfSize:20.0];
    ageLa.textColor = [UIColor whiteColor];
    ageLa.text = [profile valueForKey:@"oldyears"];
    [_bigShareImgV addSubview:ageLa];
    [ageLa release];
    
    //所在地
    UILabel * addressLa = [[UILabel alloc]initWithFrame:CGRectMake(nameLa.frame.origin.x + nameLa.frame.size.width + 84, 98 * 2, 280, 26)];
    addressLa.backgroundColor = [UIColor clearColor];
    addressLa.font = [UIFont boldSystemFontOfSize:20.0];
    addressLa.textColor = [UIColor whiteColor];
    addressLa.text = [profile valueForKey:@"incityofname"];
    [_bigShareImgV addSubview:addressLa];
    [addressLa release];
    
    //半圆号
    UILabel * idLa = [[UILabel alloc]initWithFrame:CGRectMake(63 * 2, 115 * 2, 150 * 2, 13 * 2)];
    idLa.backgroundColor = [UIColor clearColor];
    idLa.font = [UIFont boldSystemFontOfSize:20.0];
    idLa.textColor = [UIColor whiteColor];
    idLa.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"s0c0", nil),MyAppDataManager.useruid];
    [_bigShareImgV addSubview:idLa];
    [idLa release];
}
//截屏，并截取部分截屏图片
- (UIImage *)getImageFromScreenWithFrame:(CGRect)rect andView:(UIView*)screen
{
    UIGraphicsBeginImageContext(screen.bounds.size);
    //截屏
    [screen.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
//    //获取需要模糊的区域的图片
//    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
//    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    
    return image;
}
#pragma mark - 关闭按钮触发方法
/**
 * @函数名称：dissTolastView
 * @函数描述：点击关闭按钮
 * @输入参数：
 * @输出参数：
 * @返回值：
 * @author：yinlinlin
 */

- (void)dissTolastView
{
    [self dismissViewControllerAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
